package com.xinz.protocal;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;

public class Buffer {

	public byte[] buff;
	
	public int offset;
	
	public int length;
	
	public int type;
	
	final static int TYPE_LENGTH = 1;
	
	public Buffer(byte[] buff, int offset, int length) {
		super();
		this.buff = buff;
		this.offset = offset;
		this.length = length;
	}
	
	public Buffer(byte[] buff, int type) {
		this(buff,0,buff.length);
		this.type = type;
	}
	
	public Buffer(byte[] buff) {
		this(buff, 0, buff.length);
	}
	
	public byte[] getData() {
		return buff;
	}
	
	public void reset(int start, int size) {
		this.offset = start;
		this.length = size;
	}
	
	public void writeToStream(OutputStream out) throws IOException {
		out.write(buff, offset, length);
	}
	
	public ByteArrayInputStream asInputStream() {
		return new ByteArrayInputStream(buff, offset, length);
	}
	
	public ByteBuffer asByteBuffer() {
		return ByteBuffer.wrap(buff, offset, length);
	}
	
	public int getOffset() {
		return offset;
	}
	
	public int getLength() {
		return length;
	}
	
	public int getByte() {
		return buff[offset++];
	}
	
	public int getSize() {
		return length + TYPE_LENGTH;
	}
}
